package com.apes.fn.tms.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.tms.model.BranchLine;
import com.apes.fn.tms.model.BranchLineItem;
import com.apes.fn.tms.repository.BranchLineRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

@Service("branchLineService")
public class BranchLineService extends DomainService {
    @Autowired
    BranchLineRepository branchLineRepository;

    public BranchLine create(SimpleRequest simpleRequest){
        BranchLine branchLine = simpleRequest.getO(BranchLine.class);
        checkBranchLine(branchLine);
        return branchLineRepository.saveAndFlush(branchLine);
    }

    public BranchLine update(SimpleRequest simpleRequest){
        BranchLine branchLine = simpleRequest.getO(BranchLine.class);
        checkBranchLine(branchLine);
        return branchLineRepository.saveAndFlush(branchLine);
    }

    private String delete(SimpleRequest simpleRequest){
        Integer versions = simpleRequest.get("version");
        BranchLine branchLine = simpleRequest.getO(BranchLine.class);
        if (branchLine.getVersion() != versions) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        try {
            branchLineRepository.delete(branchLine);
        } catch (Exception e) {
            if (e.getMessage().indexOf("ORA-02292") != -1) throw new RuntimeException("数据已使用，不能删除");
            throw new RuntimeException(e.getMessage());
        }
        return "删除成功";
    }

    public Page<BranchLine> findAllByMainLine(SimpleRequest request){
        List<Map> filters = request.get("condition");
        JSONObject djzt = new JSONObject();
        djzt.put("field","lineType");
        djzt.put("operator","EQ");
        djzt.put("value","MainLine");
        filters.add(djzt);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return branchLineRepository.findAll(JpaDsl.toCriteria(filters),PageRequest.of(request.get("page"), request.get("size"),sort));
    }
    public Page<BranchLine> findAllByBranchLine(SimpleRequest request){
        List<Map> filters = request.get("condition");
        JSONObject djzt = new JSONObject();
        djzt.put("field","lineType");
        djzt.put("operator","EQ");
        djzt.put("value","BranchLine");
        filters.add(djzt);
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return branchLineRepository.findAll(JpaDsl.toCriteria(filters),PageRequest.of(request.get("page"), request.get("size"),sort));
    }

    public void checkBranchLine(BranchLine branchLine){
       if (branchLine.getLineType().equals("BranchLine")){
           List<BranchLineItem> branchLineItemList = branchLine.getBranchLineItems();
           branchLineItemList.sort(Comparator.comparing(BranchLineItem::getExecutionNumber));
           for (int i=0; i < branchLineItemList.size()-1; i++) {
               if (i < branchLineItemList.size()-1) {
                   if (branchLineItemList.get(i).getDuration() >= branchLineItemList.get(i+1).getDuration())throw new RuntimeException("执行顺序号越大，线路时长也越大！");
               }
           }
       }
    }

    public Page<BranchLine> findAll(SimpleRequest request) {
        List<Map> filters = request.get("condition");
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return branchLineRepository.findAll(JpaDsl.toCriteria(filters),PageRequest.of(request.get("page"), request.get("size"),sort));
    }
}
